package com.qwt.part_time_api.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.qwt.part_time_api.error.BusinessException;
import com.qwt.part_time_api.utils.EmailUtils;
import com.qwt.part_time_api.vo.Result;
import com.qwt.part_time_api.vo.params.CodeParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 君子慎独
 * @create 2022/1/22 0022 0:12
 */
@RestController
@RequestMapping("/code")
@Api(tags = {"验证码"})
public class CodeController {

    @Autowired
    private EmailUtils emailUtils;

    /**
     * 发送验证码
     *
     * @param params 参数
     * @return 验证码
     */
    @PostMapping("/sendCode")
    @ApiOperation(value = "发送验证码", notes = "用于给用户邮箱发送验证码")
    @ApiOperationSupport(author = "Tzeao")
    @ApiResponses(@ApiResponse(code = 200, message = "成功"))
    public Result sendCode(@RequestBody CodeParams params) {
        boolean b = emailUtils.sendCode(params);
        return Result.success(b);
    }

    /**
     * 校验验证码
     *
     * @param params 验证码
     * @return y/n
     * @throws BusinessException 异常
     */
    @PostMapping("/checkCode")
    @ApiOperation(value = "校验验证码", notes = "用于给验证前端给的验证码")
    @ApiOperationSupport(author = "Tzeao")
    @ApiResponses(@ApiResponse(code = 200, message = "成功"))
    public Result checkCode(@RequestBody CodeParams params) throws BusinessException {
        boolean b = emailUtils.checkCode(params);
        return Result.success(b);
    }
}
